import { protocol } from 'electron';
import { Environment } from '../environment/environment';

/**
 *
 *
 * @author chitanda
 * @date 2022-06-09 15:06:50
 * @export
 * @class ProtocolHelper
 */
export class ProtocolHelper {
  /**
   * Creates an instance of ProtocolHelper.
   *
   * @author chitanda
   * @date 2022-06-09 15:06:21
   */
  constructor() {
    this.on();
  }

  on(): void {
    protocol.registerHttpProtocol('http', (request, callback) => {
      let { url } = request;
      if (url.startsWith('http://appcenter.fetch')) {
        url = url.replace('http://appcenter.fetch', Environment.AppCenterBackendUrl);
      } else if (url.startsWith('http://mosdynamic.fetch')) {
        url = url.replace('http://mosdynamic.fetch', Environment.MosDynamicBackendUrl);
      }
      callback({ url });
    });
  }
}
